package be.phl.mycinema.fragments;

import java.util.ArrayList;
import java.util.Collection;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import be.phl.mycinema.R;
import be.phl.mycinema.Utils;
import be.phl.mycinema.adapters.MyAccountAdapter;
import be.phl.mycinema.classes.Film;
import be.phl.mycinema.classes.ReadJsonTask;

import com.google.gson.reflect.TypeToken;

public class AccountFragment extends Fragment {

	private static final String TAG = "ACCOUNT FRAGMENT";
	private ArrayList<Film> films;
	private ListView myList;
	private ImageView imageNoFilm;
	private TextView noFilm;
	private TextView noFilm2;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Log.d(TAG, "Fragment Account created.");
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.fragment_account, container, false);
		
		myList = (ListView) v.findViewById(R.id.account_listView);
		imageNoFilm = (ImageView) v.findViewById(R.id.account_image_nofilms);
		noFilm = (TextView) v.findViewById(R.id.account_nofilms);
		noFilm2 = (TextView) v.findViewById(R.id.account_nofilms2);
		
		try {
			if (Utils.isWebserviceAvailable()) {
				
				SharedPreferences prefs = getActivity().getSharedPreferences("be.phl.mycinema.userinfo",Context.MODE_PRIVATE);
				ReadJsonTask<Film> a = new ReadJsonTask<Film>(getActivity(), TAG, new TypeToken<Collection<Film>>() {}.getType());
				films = a.execute(String.format(Utils.WEBSERVICE_BASE_ADDRESS + "/gebruiker/%s/films", prefs.getString("Gebruiker_ID","0"))).get();
				
				if (films.size() != 0) {
					imageNoFilm.setVisibility(View.GONE);
					noFilm.setVisibility(View.GONE);
					noFilm2.setVisibility(View.GONE);
					myList.setVisibility(View.VISIBLE);
					
					MyAccountAdapter adapter = new MyAccountAdapter(v.getContext(), films,prefs.getString("Gebruiker_ID","0"));
					myList.setAdapter(adapter);
				} else {
					imageNoFilm.setVisibility(View.VISIBLE);
					noFilm.setVisibility(View.VISIBLE);
					noFilm2.setVisibility(View.VISIBLE);
					myList.setVisibility(View.GONE);
				}

			} else {
				Toast.makeText(getActivity().getBaseContext(), "De server is niet bereikbaar, probeer later opnieuw.", Toast.LENGTH_LONG).show();
				getActivity().finish();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		Log.d(TAG, "View Account created.");
		return v;		
	}
	    
	@Override
	public void onDestroyView() {
		Log.d(TAG, "Accountfragement destroyed");
		super.onDestroyView();
	}
}
